<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 8:56
 */

namespace app\admin\controller;


use cmf\controller\AdminBaseController;

class ConsumesController extends AdminBaseController
{

    public function index()
    {
        $registerModel = db('register');
        $consumeModel = db('consume_logs');
        /**
         * 查询user_id用户下的所有用户的消费记录
         */
        $user_id = $this->request->param('id', 1, 'int');
        $year = $this->request->param('year', null);
        $moth = $this->request->param('moth', 0, 'int');
        if(empty($year))
            $year = date('Y');
        if (empty($moth))
            $moth = date('m');

        //1.查询user_id下的所有用户id
        $userInfo = $registerModel->alias('r')
            ->field('r.*, ra.bonus, ra.base_money, ra.cell')
            ->join('hstht_rank ra', 'r.lev = ra.id', 'LEFT')
            ->where('r.id', $user_id)->find();
        //$ids = $registerModel->where('pid', $user_id)->column('id');

//        //获取前五年的数组
//        $years = getFiveYears();
//        //获取12月的月份
//        $moths = getMoths();

        //2.根据查到的用户id数组查询所有用户某个月消费的记录总和
        $list1 = getRegisterMothTotalMoney($user_id, $year);

        /*//通过每月消费的总额，给月份数组moths赋值
        foreach ($list1 as $value) {
            if ($value['moth'] == $moths[$value['moth']]['moth']){
                $moths[$value['moth']]['total_money'] = $value['shouyi'];
            }
        }*/

        //3.查询某个月的每条消费记录
        $field = "c.moth, c.register_id, c.year, c.money, c.create_time, ra.bonus, ra.base_money, r.sex, r.name, r.phone, g.goods_name";
        $join = [
            ['hstht_consume_logs c', 'i.consu_id = c.id', 'LEFT'],
            ['hstht_register r', 'r.id=c.register_id', 'LEFT'],
            ['hstht_goods g', 'g.id=c.goods_id', 'LEFT'],
            ['hstht_rank ra', 'ra.id=c.rank_id', 'LEFT'],
        ];

        $where = array(
            'i.inco_reg_id' => $user_id,
//            'c.register_id' => array('in', $ids),
            'c.status'    => 1,
            'c.year'    => $year,
            //'c.moth'    => $moth
        );

        $list = db('income')->alias('i')->field($field)->join($join)->where($where)->paginate(1000);
        foreach ($list1 as $key=>$val) {
            foreach ($list as $k=>$v){
                if ($val['moth'] == $v['moth']){
                    $list1[$key]['sub'][] = $v;
                }
            }
        }
        $page = $list->render();
        return $this->fetch('index', array(
            'list' => $list1,
            //'years' => $years,
            'userInfo' => $userInfo,
            'year' => $year,
            'page' => $page,
            'moth' => $moth,
            //'moths' => $moths
        ));
    }

    /**
     * @return mixed
     * 此方法暂时没用
     */
    public function consumeList()
    {
        $registerModel = db('register');
        $consumeModel = db('consume_logs');
        /**
         * 查询每个用户每月消费记录
         */
        $user_id = $this->request->param('id', 0, 'int');
        $moth = $this->request->param('moth', 0, 'int');
        $year = $this->request->param('year', 0, 'int');

        if(empty($year))
            $year = date('Y');
        if (empty($moth))
            $moth = date('m');

        //查询用户下的所有用户id
        $ids = $registerModel->where('pid', $user_id)->column('id');
        $userInfo = $registerModel->alias('r')->field('r.*, ra.bonus, ra.base_money, ra.cell')->join('hstht_rank ra', 'r.lev = ra.id', 'LEFT')->where('r.id', $user_id)->find();

        //2.根据查到的用户id数组查询所有用户某个月消费的记录总和
        $field = "c.id, c.moth, c.register_id, c.year, r.sex, r.name, r.phone";
        $join = [
            ['hstht_register r', 'r.id=c.register_id', 'LEFT']
        ];
        $where = array(
            'c.register_id' => array('in', $ids),
            'c.status'    => 1,
            'c.year'    => $year,
            'c.moth'    => $moth
        );

        $list = $consumeModel->alias('c')->field($field)->join($join)->where($where)->paginate(20);
        $page = $list->render();

        return $this->fetch('', array(
            'list' => $list,
            'page' => $page,
            'userInfo' => $userInfo,
            'moth' => $moth
        ));
    }

}